﻿package adpter.layer
{
	import adapter.common.AdapterBase;
	
	import adpter.GameUIAdapter;
	
	import datapool.Context;
	import datapool.define.IDZoneDefine;
	import datapool.event.EventObject;
	import datapool.global.GlobalInfo;
	import datapool.map.MapInfo;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import ui.layer.MapLayerUI;
	
	public class MapLayerAdapter extends AdapterBase
	{
		private var _bmd:BitmapData;
		private var _uiReady:Boolean   = false;
		private var _dataReady:Boolean = false;
		private var _parentAdapter:GameUIAdapter;
		
		static public var mapWidth:int;
		static public var mapHight:int;
		
		public function MapLayerAdapter()
		{
			super();
		}
		
		public function get uiMapLayer():MapLayerUI
		{
			return ui as MapLayerUI;
		}
		
		public function set parentAdapter(gameUIAdapter:GameUIAdapter):void
		{
			_parentAdapter = gameUIAdapter;
		}
		
		public function get bmd():BitmapData
		{
			return _bmd;
		}
		
		override protected function onUIAttach():void
		{
			super.onUIAttach();
			_uiReady = true;			
			checkAndUpdate();
		}
		
		override protected function onUIDeattach():void
		{
			_uiReady = false;
			super.onUIDeattach();
		}

		public function update(mapId:uint = 0x00200000):void
		{
			var mapInfo:MapInfo = context.mapPool.getMapInfoById(mapId);
			if(mapInfo)
			{
				context.remoteResource.requestRs(mapInfo.mapUrl, onLoadComplete);
			}
		}
		
		private function onLoadComplete(data:*):void
		{
			var bmp:Bitmap = data as Bitmap;
			if(bmp)
			{
				_bmd       = bmp.bitmapData;
				mapWidth  = _bmd.width;
				mapHight  = _bmd.height;
				_dataReady = true;
				checkAndUpdate();
			}
		}
		
		public function checkAndUpdate():void
		{
			if(_uiReady && _dataReady)
			{
				refreshNotify();
			}
		}
		
		public function gotoPos(pos:Point):void
		{
			if(_parentAdapter && pos)
			{
				_parentAdapter.gotoPos(pos);
			}
		}
		
		public function updateViewRect():void
		{
			var pos:Point;	
			if(uiMapLayer)
			{				
				uiMapLayer.x = -GlobalInfo.oppositePosX;
				uiMapLayer.y = -GlobalInfo.oppositePosY;
			}
		}
	}
}